#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
/* widgets:
Layer_Heat
Background_Heat
Container_Heat_On
Container_Heat_Text
Text_Heat_Baifenhao
Text_Heat_Liangdu
Container_Heat_Button
PopupButton_Heat_OFF
Meter_Heat_Xuanzhuan
Icon_Heat_Pointer
CircleProgressBar_Heat_Temp
Icon_Heat_Jingdu
Text_Heat_TempNum
Container_Heat_Off
Text_Heat_OFF
PopupButton_Heat_ON
Icon_Heat_JindutiaoOff
Button_Back_MainMenu
*/
static ITUBackground* Background_Heat;
static ITUContainer* Container_Heat_Off;
static ITUContainer* Container_Heat_On;
static ITUMeter* Meter_Heat_Xuanzhuan;
static ITUCircleProgressBar* CircleProgressBar_Heat_Temp;
static ITUText	*Text_Heat_TempNum;
static ITUText	*Text_Heat_Wendu;
static int MeterValue = 0, MeterValue_L = 0;
bool HeatOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_Heat)//首次加载
	{
		Background_Heat = ituSceneFindWidget(&theScene, "Background_Heat");
		assert(Background_Heat);
		Container_Heat_Off = ituSceneFindWidget(&theScene, "Container_Heat_Off");
		assert(Container_Heat_Off);
		Container_Heat_On = ituSceneFindWidget(&theScene, "Container_Heat_On");
		assert(Container_Heat_On);
		Meter_Heat_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_Heat_Xuanzhuan");
		assert(Meter_Heat_Xuanzhuan);
		CircleProgressBar_Heat_Temp = ituSceneFindWidget(&theScene, "CircleProgressBar_Heat_Temp");
		assert(CircleProgressBar_Heat_Temp);
		Text_Heat_TempNum = ituSceneFindWidget(&theScene, "Text_Heat_TempNum");
		assert(Text_Heat_TempNum);
		Text_Heat_Wendu = ituSceneFindWidget(&theScene, "Text_Heat_Wendu");
		assert(Text_Heat_Wendu);
	}
	//更具参数初始化界面
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.status == 1)//打开
	{
		ituWidgetSetVisible(Container_Heat_Off, false);
		ituWidgetSetVisible(Container_Heat_On, true);
		
	}
	else//关闭
	{
		ituWidgetSetVisible(Container_Heat_On, false);
		ituWidgetSetVisible(Container_Heat_Off, true);
	}
	//设置温度值
	ituMeterSetValue(Meter_Heat_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.Set_Temp / 2 - Heat_Temp_Min);
	//对标题进行语言转换
	if (Sys.Language == MG_LANGUAGE_CHS || Sys.Language == MG_LANGUAGE_ENG)
	{
		Text_Heat_Wendu->fontIndex = 0;
		Text_Heat_Wendu->boldSize = 1;
		ituTextSetFontSize(Text_Heat_Wendu, 18);
	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		Text_Heat_Wendu->fontIndex = 8;
		Text_Heat_Wendu->boldSize = 0;
		ituTextSetFontSize(Text_Heat_Wendu, 16);
	}
	//Text操作
	char TextString[20];
	itoa(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.Set_Temp / 2, TextString, 10);//转换成字符串
	ituTextSetString(Text_Heat_TempNum, TextString);//显示字符
	MeterValue = Meter_Heat_Xuanzhuan->value;
	MeterValue_L = MeterValue;
	Set_Rotate_Block(60);//旋转步进
    return true;
}

bool HeatOnLeave(ITUWidget* widget, char* param)
{
    return true;
}

bool HeatOnTimer(ITUWidget* widget, char* param)
{
	static uint32_t SendEndTime = Rotate_SendCmd_Time;

	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}

	//设备操作
	MeterValue = Meter_Heat_Xuanzhuan->value;
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.status == 1)//设备开
	{
		if (Action_Cmd.User_Action == Clockwise_Rotation)//有用户操作
		{
			MeterValue += Action_Cmd.Action_Times;
			if (MeterValue > 25)MeterValue = 25;
			ituMeterSetValue(Meter_Heat_Xuanzhuan, MeterValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation)
		{
			MeterValue -= Action_Cmd.Action_Times;
			if (MeterValue < 0)MeterValue = 0;
			ituMeterSetValue(Meter_Heat_Xuanzhuan, MeterValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}
	}
	//结束操作设备状态执行
	if (MeterValue == MeterValue_L)
	{
		if (SendEndTime <= Rotate_SendCmd_Time)
			SendEndTime++;
		//停止操作0.6s后发送执行报文
		if (SendEndTime == Rotate_SendCmd_Time)
		{
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
		}
	}
	else
	{
		SendEndTime = 0;
		MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.Set_Temp = (MeterValue + Heat_Temp_Min) * 2;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.status = CMD_HEAT_SETTEMP;//设置温度
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.Set_Temp = (MeterValue + Heat_Temp_Min) * 2;
	}
	//设备状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.status == 1)//打开
		{
			ituWidgetSetVisible(Container_Heat_Off, false);
			ituWidgetSetVisible(Container_Heat_On, true);
			//设置温度值
			ituMeterSetValue(Meter_Heat_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.Set_Temp / 2 - Heat_Temp_Min);
		}
		else//关闭
		{
			ituWidgetSetVisible(Container_Heat_On, false);
			ituWidgetSetVisible(Container_Heat_Off, true);
		}
		MeterValue = Meter_Heat_Xuanzhuan->value;//防止设备循环上报
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	MeterValue_L = MeterValue;

	//Text操作
	char TextString[20];
	itoa(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.Set_Temp / 2, TextString, 10);//转换成字符串
	ituTextSetString(Text_Heat_TempNum, TextString);//显示字符
    return true;
}

bool PopupButton_Heat_OFF_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.status = 0;//状态关
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.status = CMD_HEAT_OFF;//命令关
	printf("Moorgen INFO : Touch to turn off the Heat\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

bool PopupButton_Heat_ON_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.status = 1;//状态开
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Heat_Status.status = CMD_HEAT_ON;//命令开
	printf("Moorgen INFO : Touch to turn on the Heat\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}
bool Meter_Heat_Xuanzhuan_Changed(ITUWidget* widget, char* param)
{
	ituCircleProgressBarSetValue(CircleProgressBar_Heat_Temp, Meter_Heat_Xuanzhuan->value * 4);
	return true;
}
